package com.itheima.web;

import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import com.itheima.vo.MenuAndRoleVo;
import com.itheima.vo.MenuVo;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：yuanjian
 * @date ：Created in 2020/11/22 12:13
 * @description：
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

   /* @GetMapping("getMenus")
//    @PreAuthorize("hasAuthority('getMenus')")
    public Result getMenus(){
        return new Result(true,"",menuService.getMenus());
    }*/

    @GetMapping("/getMenus/{username}")
//    @PreAuthorize("hasAuthority('getMenus')")
    public Result getMenus(@PathVariable("username") String username){
        List<MenuVo> menus = menuService.getMenus(username);
        return new Result(true,"",menus);
    }

    @GetMapping("/getRoleByMenuId/{menuId}")
    public Result getRoleByMenuId(@PathVariable("menuId") String menuId){
        long id = Long.parseLong(menuId);
        List<Long> roleIds = menuService.getRoleByMenuId(id);
        return new Result(true,"",roleIds);
    }

    @PostMapping("/updateRoles")
    public Result  updateRoles(@RequestBody MenuAndRoleVo menuAndRoleVo){
//        System.out.println("111");
        menuService.updateRoles(menuAndRoleVo);
        return new Result(true,"");
    }

    @GetMapping("/getRole")
    public Result getRole(){
        List<Role> roles = menuService.getRole();
        return new Result(true,"",roles);
    }

}
